package com.zhangdi.dwdata.page;

import com.zhangdi.dwdata.query.SimplePage;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhangdi on 2020/09/18 17:07:26.
 */
public class Pagination<T> extends SimplePage implements Serializable,
        Paginable {

    public Pagination() {
    }

    public Pagination(int pageNo, int pageSize, int totalCount) {
        super(pageNo, pageSize, totalCount);
        if (totalCount < 1) {
            list = new ArrayList<>();
        }
    }

    int size = 2;
    private List<T> list;

    public Pagination(int pageNo, int pageSize, int totalCount, List<T> list) {
        super(pageNo, pageSize, totalCount);
        this.list = list;
    }

    public int getFirstResult() {
        return (pageNo - 1) * pageSize;
    }

    public int getFirstNo() {
        int result = 1;
        result = pageNo - size;

        int t = getEndNo() - result;
        int num = size * 2;
        if (t < num) {
            result = result - (num - t);
        }
        if (result <= 0) {
            result = 1;
        }
        return result;
    }

    public int getEndNo() {
        int result = 1;
        result = getPageNo() + size;

        if (getPageNo() <= size) {
            result = result + size - getPageNo();
            result++;
        }


        if (result > getTotalPage()) {
            result = getTotalPage();
        }
        return result;
    }

    public List<T> getList() {
        return list;
    }

    @SuppressWarnings("unchecked")
    public void setList(List list) {
        this.list = list;
    }
}
